package com.SmartLED.Service;

import com.SmartLED.Common.SYS_Base;
import com.SmartLED.Common.Server_config;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Frozen on 2017/1/9.
 */
public class BaseService extends SYS_Base {
    //返回数据
    public String result = "";

    public String sendResult() throws JsonProcessingException {
        if(!errorCode.isEmpty()){
            return sendErrorByCode(errorCode);
        }
        if(result.isEmpty()){
            setDefaultResult();
        }
        return result;
    }

    //设置返回结果
    public void setResult(String msg, Object data) throws JsonProcessingException {
        Map<String, Object> map = new HashMap<String,Object>();
        map.put("code", "0");
        map.put("msg", msg);
        map.put("data", data);
        this.result = new ObjectMapper().writeValueAsString(map);
    }

    //默认返回结果
    public void setDefaultResult() throws JsonProcessingException {
        this.setResult("", "");
    }

    //将数据库中的路径转换成发送给前端的URL路径
    public String getUrl(String filePath){
        return Server_config.file_server_url +filePath;
    }

    //读取文件
    public String readFile(String filePath){
        filePath = Server_config.file_root+filePath;
        System.out.println("测试路径+++++++" + filePath);
        String result = "";
        try{
            InputStreamReader read = new InputStreamReader(new FileInputStream(filePath));//考虑到编码格式
            BufferedReader bufferedReader = new BufferedReader(read);
            String lineTxt = "";
            while((lineTxt = bufferedReader.readLine()) != null){
                result += lineTxt;
            }
            read.close();
        }
        catch (Exception e){
            e.printStackTrace();
            this.setErrorCode("E_1003");
        }
        return result;
    }

    //删除文件,并不实际删除,而是保留在deleted目录下,可以恢复
    public boolean deleteFile(String filePath){
        filePath = Server_config.file_root+filePath;
        String newPath = Server_config.file_delete_root+filePath;
        File file = new File(filePath);
        return file.renameTo(new File(newPath));
    }

    //创建Base64加密后的文件
    public boolean createBase64File(String filePath, String fileData){
        filePath = Server_config.file_root+filePath;
        try{
            checkFile(filePath);
            OutputStream out = new FileOutputStream(filePath);
            byte[] b = Base64.decodeBase64(fileData.getBytes());
            out.write(b);
            out.flush();
            out.close();
        }
        catch(IOException e){
            this.setErrorCode("E_1003");
        }
        return true;
    }

    //根据 request 创建图片
    public boolean createImageFile(String filePath,MultipartFile img)
    {
        try {
            filePath = Server_config.file_root + filePath;
            checkFile(filePath);
            OutputStream out = new FileOutputStream(filePath);
            out.write(img.getBytes());
            out.flush();
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            setErrorCode("E_1003");
        }
        return true;
    }

    //创建文件,会覆盖之前的数据 TODO 创建公共函数 放在公共函数中
    public boolean createFile(String filePath, String fileData){
        filePath = Server_config.file_root+filePath;
        System.out.println("文件路径+++++++" + filePath);
        try{
            checkFile(filePath);
            OutputStream out = new FileOutputStream(filePath);
            out.write(fileData.getBytes());
            out.flush();
            out.close();
        }
        catch(IOException e){
            this.setErrorCode("E_1003");
        }
        return true;
    }

    //判断文件是否存在,不存在则创建,会创建路径
    public void checkFile(String filePath) throws IOException{
        File file =new File(filePath);
        createFileDir(file.getParent());
        if( !file.exists() ){
            file.createNewFile();
        }
    }

    //递归创建文件路径,这里只会创建文件夹
    private void createFileDir(String dirPath){
        File file =new File(dirPath);
        if( !file.exists() ){
            createFileDir(file.getParent());
            file.mkdir();
        }
    }
}
